//
//  WatchFocusView.m
//  RunningLabels
//
//  Created by Zeus on 16/4/11.
//  Copyright © 2016年 Hera. All rights reserved.
//

#import "WatchFocusView.h"
#define HORIZONTAL_PAD   6.0f
#define VERTICAL_PAD     6.0f
#define LABEL_MARGIN     6.0f
#define BOTTOM_MARGIN    6.0f
@implementation WatchFocusView
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        totalHeight = 0;
    }
    return self;
}
// 标签赋值
-(void)setTagWithTagArray:(NSArray*)tagArray
{
    previousFrame = CGRectZero;
    // 一边便利数组，又同时修改这个数组里面的内容，会导致崩溃
    // 解决方法定义一个一模一样的数组，便利数组A然后操作数组B
    NSArray * array = [NSArray arrayWithArray: tagArray];
    NSMutableArray *focusArray = [NSMutableArray array];
    for (focusModel *focusModel in array) {
        // 装载标签名
        [focusArray addObject:focusModel.name];
    }
    [focusArray enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        UILabel *tag =[[UILabel alloc]initWithFrame:CGRectZero];
        tag.textColor = [UIColor colorWithRed:96/255.0 green:111/255.0 blue:168/255.0 alpha:1];
        tag.textAlignment = NSTextAlignmentCenter;
        tag.font = [UIFont systemFontOfSize:12];
        tag.text = str;
        tag.layer.cornerRadius = 2;
        tag.clipsToBounds = YES;
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12]};
        CGSize Size_str=[str sizeWithAttributes:attributes];
        Size_str.width += HORIZONTAL_PAD;
        Size_str.height += VERTICAL_PAD;
        CGRect newRect = CGRectZero;
        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
            newRect.origin = CGPointMake(5, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
            totalHeight +=Size_str.height + BOTTOM_MARGIN;
        }
        else {
            newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
        }
        newRect.size = Size_str;
        [tag setFrame:newRect];
        previousFrame=tag.frame;
        [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        [self addSubview:tag];
    }];
    if(_viewBackgroundColor){
        self.backgroundColor = _viewBackgroundColor;
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
}

#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)hight
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = hight;
    view.frame = tempFrame;
}

@end
